﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Riel_Gestion.entities;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
using System.Windows.Forms;

namespace Riel_Gestion.dao
{
    class CompraDAO
    {
        private static String SQL_SELECT_SALDOS_BY_ID = "SELECT  SUM(IMPORTEVENTA) AS TOTDEBE, SUM(IMPORTEPAGO) AS TOTHABER FROM COMPRA WHERE CODPROVEEDOR = ?";
        private static String EMPTY_FIELD = "";

        private static OleDbConnection conn;
        private static OleDbCommand cmd;
        private static OleDbDataAdapter adapter;
        private static OleDbDataReader reader;
        private static OleDbCommandBuilder cmdbl;

        public static DTOSaldo getSaldosTotalesById(String idCliente)
        {
            DTOSaldo saldo = null;
            String value = "";
            try
            {
                saldo = new DTOSaldo(idCliente);
                conn = conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["rielConnectionString"].ConnectionString);
                cmd = new OleDbCommand(SQL_SELECT_SALDOS_BY_ID, conn);
                conn.Open();

                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("CODPROVEEDOR", idCliente);

                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    value = reader["TOTDEBE"].ToString();
                    if (!EMPTY_FIELD.Equals(value))
                        saldo.TotDebe = Decimal.Parse(value);

                    value = reader["TOTHABER"].ToString();
                    if (!EMPTY_FIELD.Equals(value))
                        saldo.TotHaber = Decimal.Parse(value);

                    saldo.TotSaldo = saldo.TotDebe - saldo.TotHaber;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return saldo;
        }




    }
}
